//
//  VideoCaptureViewController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/8.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "VideoCaptureViewController.h"

#import "VideoCaptureViewController.h"
#import "VideoCaptureManager.h"
#import "VideoCaptureActionView.h"
#import "VideoCaptureProgressView.h"
#import "ChooseMusicController.h"
#import "VideoListController.h"
#import "VideoPlayView.h"

#import "VideoReleseController.h"

@interface VideoCaptureViewController ()

@property (strong, nonatomic) VideoCaptureManager *captureManager;
@property (strong, nonatomic) UIView *previewView;
/**显示相册列表*/
@property (nonatomic,strong)VideoListController*videoListView;
/**<#属性#>*/
@property (nonatomic,strong)VideoPlayView*playView;
/// 取消
@property (strong, nonatomic) UIButton *backBtn;

/// 手电筒开关
//@property (strong, nonatomic) UIButton *flashBtn;

/**拍摄*/
@property (nonatomic,strong)UIButton*shootingBtn;
/**相册*/
@property (nonatomic,strong)UIButton*photoAlbumBtn;
/// 切换摄像头
@property (strong, nonatomic) UIButton *switchCameraBtn;
/**下一步*/
@property (nonatomic,strong)UIButton*nextBtn;

@property (assign, nonatomic) BOOL isFlashLightOn;

@property (strong, nonatomic) VideoCaptureActionView *actionView;
/**<#属性#>*/
@property (nonatomic,strong)VideoCaptureProgressView*progressView;
/**<#属性#>*/
@property (nonatomic,strong)UIView*lineView;
/**音乐*/
@property (nonatomic,strong)CustomButton*musicBtn;
@end

@implementation VideoCaptureViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorFromRGB(0x282828);
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self.captureManager setupSession];
    [self.view addSubview:self.previewView];
    [self.previewView.layer insertSublayer:self.captureManager.captureVideoPreviewLayer atIndex:0];
   
    [self.view addSubview:self.switchCameraBtn];
//    [self.view addSubview:self.flashBtn];
    [self.view addSubview:self.backBtn];
    [self.view addSubview:self.progressView];
    [self.view addSubview:self.shootingBtn];
    [self.view addSubview:self.photoAlbumBtn];
    [self.view addSubview:self.actionView];
//    [self.view addSubview:self.lineView];
    [self.view addSubview:self.musicBtn];
    [self.view addSubview:self.nextBtn];
    [self.view addSubview:self.videoListView.view];
    [self.view addSubview:self.playView];

    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(7);
        make.right.mas_equalTo(-7);
        make.top.mas_equalTo(45);
        make.height.offset([VideoCaptureProgressView viewHeight]);

    }];
    
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.mas_equalTo(self.progressView.mas_bottom).offset(18);
//        if (@available(iOS 11.0, *)) {
//            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(20);
//        } else {
//            make.top.offset(20);
//        }
        make.size.sizeOffset(CGSizeMake(40, 40));
    }];
    
    [self.shootingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backBtn);
        make.centerX.mas_equalTo(-40);
    }];
    [self.photoAlbumBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backBtn);
        make.centerX.mas_equalTo(40);
    }];

    [self.actionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
//        if (@available(iOS 11.0, *)) {
//            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset([self previewSize].height);
//            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
//        } else {
//            make.top.offset([self previewSize].height);
//            make.bottom.offset(0);
//        }
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(120);
    }];
    
    [self.videoListView.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        CGFloat width = (KScreenWidth - 3)/4;
        make.height.mas_equalTo(width * 2 + 10);
    }];
    [self.playView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.shootingBtn.mas_bottom).offset(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.videoListView.view.mas_top);
        
    }];
    [self.switchCameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.top.equalTo(self.backBtn);
        make.size.sizeOffset(CGSizeMake(40, 40));
    }];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.switchCameraBtn);
        make.width.mas_equalTo(58);
        make.height.mas_equalTo(28);
    }];
//    [self.flashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.switchCameraBtn.mas_left).offset(-30);
//        make.top.equalTo(self.backBtn);
//        make.size.sizeOffset(CGSizeMake(40, 40));
//    }];
//    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(0);
//        make.height.mas_equalTo(0.5);
//        make.top.mas_equalTo(self.backBtn.mas_bottom).offset(20);
//    }];
    [self.musicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-16);
        make.top.mas_equalTo(self.switchCameraBtn.mas_bottom).offset(40);;
    }];
}

- (void)viewWillLayoutSubviews{
    
    [super viewWillLayoutSubviews];
    
    CGSize size = [self previewSize];
    
    CGFloat topMargin = 0;
    
//    if (@available(iOS 11.0, *)) {
//        topMargin = self.view.safeAreaInsets.top;
//    }
    self.previewView.frame = CGRectMake(0, topMargin, size.width, size.height);
        
    self.captureManager.captureVideoPreviewLayer.frame = self.previewView.bounds;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [UIApplication sharedApplication].statusBarHidden = YES;

    __weak typeof(self) _WeakSelf = self;
    void (^jumpToSettingsBlock) (void) = ^{
        if (@available(iOS 10.0, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:nil];
        } else {
            // Fallback on earlier versions
        }
        [_WeakSelf dismissViewControllerAnimated:YES completion:nil];
    };
    
    BOOL videoAuthorization = [VideoCaptureManager checkVideoAuthorization];
    BOOL audioAuthorization = [VideoCaptureManager checkAudioAuthorization];
    
    if (!videoAuthorization) {
        [UIAlertController alert:@"未获得相机使用授权" message:@"请在 '设置-隐私-相机' 中开启权限。" cancelTitle:@"确定" cancelBlock:^(UIAlertAction *action){
            jumpToSettingsBlock();
        } completionBlock:nil];
    } else if (!audioAuthorization) {
        [UIAlertController alert:@"未获得麦克风使用授权" message:@"请在 '设置-隐私-相机' 中开启权限。" cancelTitle:@"确定" cancelBlock:^(UIAlertAction *action){
            jumpToSettingsBlock();
        } completionBlock:nil];
    }
    
    if (videoAuthorization && audioAuthorization && ![Utilis isSimuLator]) {
        [self.captureManager.captureSession startRunning];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [self.captureManager.captureSession stopRunning];
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    [self.playView pausePlay];

}

#pragma mark- Getter & Setter

//- (UIButton *)flashBtn
//{
//    if (!_flashBtn) {
//        _flashBtn = [[UIButton alloc] init];
//        [_flashBtn setImage:[UIImage imageNamed:@"icon_flash_close_24x24"] forState:UIControlStateNormal];
//        [_flashBtn addTarget:self action:@selector(flashBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _flashBtn;
//}

- (void)flashBtnAction:(id)sender
{
    self.isFlashLightOn = !self.isFlashLightOn;
    
    [self.captureManager changeDevice:self.captureManager.captureDevice property:^(AVCaptureDevice *device) {
        if (self.isFlashLightOn) {
            if ([device isTorchModeSupported:AVCaptureTorchModeOn]) {
                [device setTorchMode:AVCaptureTorchModeOn];
//                [self.flashBtn setImage:[UIImage imageNamed:@"icon_flash_on_24x24"] forState:UIControlStateNormal];
            }
        } else {
            if ([device isTorchModeSupported:AVCaptureTorchModeOff]) {
                [device setTorchMode:AVCaptureTorchModeOff];
//                [self.flashBtn setImage:[UIImage imageNamed:@"icon_flash_close_24x24"] forState:UIControlStateNormal];
            }
        }
    }];
}

- (UIButton *)switchCameraBtn
{
    if (!_switchCameraBtn) {
        _switchCameraBtn = [[UIButton alloc] init];
        [_switchCameraBtn setImage:[UIImage imageNamed:@"icon_camera_white_24x24"] forState:UIControlStateNormal];
        [_switchCameraBtn addTarget:self action:@selector(switchCameraAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchCameraBtn;
}
-(UIButton*)nextBtn{
    if(!_nextBtn){
        _nextBtn=[UIButton new];
        [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        _nextBtn.titleLabel.font = SYSTEMFONT(14);
        [_nextBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x009966)] forState:UIControlStateNormal];
        _nextBtn.hidden = YES;
        kWeakSelf(weakSelf);
        [_nextBtn addActionHandler:^{
            if (!weakSelf.playView.model) {
                ShowHubMessage(@"请选择或者拍摄视频");
                return ;
            }
            VideoReleseController * vc = [VideoReleseController new];
            vc.model = weakSelf.playView.model;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _nextBtn;
}
- (void)switchCameraAction:(UIBarButtonItem *)sender
{
    [self.captureManager switchVideoDevice:nil];
}

- (UIButton *)backBtn{
    if (!_backBtn) {
        _backBtn = [[UIButton alloc] init];
//        [_backBtn setImage:[UIImage imageNamed:@"icon_close_white_24x24"] forState:UIControlStateNormal];
        [_backBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_backBtn setTitleColor:RGBA(255, 255, 255, 0.54) forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (void)backBtnAction:(id)sender{
    
    [UIAlertController alert:@"提示" message:@"是否放弃当前视频编辑？" submitTitle:@"确定" submitBlock:^(UIAlertAction * _Nonnull action) {
        [self.viewModel.videoItemList enumerateObjectsUsingBlock:^(VideoCaptureItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [MediaManager deleteVideoFile:obj.filePath];
        }];
        self.viewModel.videoItemList = nil;

//        [self backBtnClicked];
        [self dismissViewControllerAnimated:YES completion:nil];

    } cancelTitle:@"取消" cancelBlock:^(UIAlertAction * _Nonnull action) {

    } completionBlock:^{

    }];
}
-(void)backBtnClicked{
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }

}
-(CustomButton*)musicBtn{
    if(!_musicBtn){
        _musicBtn=[CustomButton buttonWithTitle:@"音乐" Image:@"音乐"];
        _musicBtn.titleColor = UIColorFromRGB(0xF5F7F5);
        _musicBtn.titleFont = SYSTEMFONT(10);
        kWeakSelf(weakSelf);
        [_musicBtn setHandler:^(CustomButton * _Nonnull view) {
            [weakSelf.navigationController pushViewController:[ChooseMusicController new] animated:YES];
        }];
    }
    return _musicBtn;
}
-(UIButton*)shootingBtn{
    if(!_shootingBtn){
        _shootingBtn=[UIButton new];
        [_shootingBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateSelected];
        [_shootingBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        [_shootingBtn setTitle:@"拍摄" forState:UIControlStateNormal];
        _shootingBtn.selected = YES;
        kWeakSelf(weakSelf);
        [_shootingBtn addActionHandler:^{
            weakSelf.shootingBtn.selected=YES;
            weakSelf.photoAlbumBtn.selected = NO;
            [weakSelf swichPreViewContex:0];
        }];
    }
    return _shootingBtn;
}
-(UIButton*)photoAlbumBtn{
    if(!_photoAlbumBtn){
        _photoAlbumBtn=[UIButton new];
        [_photoAlbumBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateSelected];
        [_photoAlbumBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        [_photoAlbumBtn setTitle:@"相册" forState:UIControlStateNormal];
        kWeakSelf(weakSelf);
        [_photoAlbumBtn addActionHandler:^{
            weakSelf.shootingBtn.selected=NO;
            weakSelf.photoAlbumBtn.selected = YES;
            [weakSelf swichPreViewContex:1];

        }];

    }
    return _photoAlbumBtn;
}
-(void)swichPreViewContex:(NSInteger)index{
    if (index == 0) {//拍摄
        self.previewView.hidden = NO;
        self.actionView.hidden = NO;
        self.musicBtn.hidden = NO;
        self.switchCameraBtn.hidden = NO;
        self.nextBtn.hidden = YES;
        self.progressView.hidden = NO;
        self.videoListView.view.hidden = YES;
        self.playView.hidden = YES;
        [self.playView pausePlay];
    }else if (index == 1){//相册
        self.previewView.hidden = YES;
        self.actionView.hidden = YES;
        self.musicBtn.hidden = YES;
        self.switchCameraBtn.hidden = YES;
        self.nextBtn.hidden = NO;
        self.progressView.hidden = YES;
        self.videoListView.view.hidden = NO;
        self.playView.hidden = NO;
        [self.videoListView reloadData];
        [self.playView playVideo];
    }
}
-(UIView*)lineView{
    if(!_lineView){
        _lineView=[UIView new];
        _lineView.backgroundColor = UIColorFromRGB(0x273323);
    }
    return _lineView;
}
- (UIView *)previewView
{
    if (!_previewView) {
        _previewView = [[UIView alloc] init];
        _previewView.layer.backgroundColor = [UIColor blackColor].CGColor;
    }
    return _previewView;
}
-(VideoListController*)videoListView{
    if(!_videoListView){
        _videoListView=[VideoListController new];
        _videoListView.view.hidden = YES;
        kWeakSelf(weakSelf);
        _videoListView.didselBlock = ^(VideoPlayModel * _Nonnull model) {
            weakSelf.playView.model = model;
        };
    }
    return _videoListView;
}
-(VideoPlayView*)playView{
    if(!_playView){
        _playView=[VideoPlayView new];
        _playView.hidden = YES;
    }
    return _playView;
}
- (CGSize)previewSize
{
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
//    return CGSizeMake(screenSize.width, screenSize.width/480*640);
    return CGSizeMake(screenSize.width, KScreenHeight);

}
-(VideoCaptureProgressView*)progressView{
    if(!_progressView){
        _progressView=[[VideoCaptureProgressView alloc] init];
        _progressView.totalProgress = 30;
    }
    return _progressView;
}
- (VideoCaptureActionView *)actionView
{
    if (!_actionView) {
        _actionView = [[VideoCaptureActionView alloc] init];
//        _actionView.videoProgressView.totalProgress = 30;

        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchDownAction:) forControlEvents:UIControlEventTouchDown];
        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchUpAction:) forControlEvents:UIControlEventTouchUpInside];
        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchUpAction:) forControlEvents:UIControlEventTouchUpOutside];
        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchUpAction:) forControlEvents:UIControlEventTouchDragOutside];
        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchUpAction:) forControlEvents:UIControlEventTouchDragExit];
        [_actionView.captureBtn addTarget:self action:@selector(captureBtnTouchUpAction:) forControlEvents:UIControlEventTouchCancel];

        [_actionView.undoBtn addTarget:self action:@selector(undoBtnActon:) forControlEvents:UIControlEventTouchUpInside];
        [_actionView.doneBtn addTarget:self action:@selector(doneBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_actionView.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _actionView;
}
#pragma mark --用户点触按钮
- (void)captureBtnTouchDownAction:(id)sender{
    DLogFunc;
    if (![self.captureManager.captureMovieFileOutput isRecording]) {
        [self.captureManager startRecordVideo:[MediaManager filePath:[MediaManager getVideoFileName]]];
    }
}

- (void)captureBtnTouchUpAction:(id)sender{
    DLogFunc;
    if ([self.captureManager.captureMovieFileOutput isRecording]) {
        [self.captureManager stopVideoRecoding];
    }
}
#pragma mark 返回
- (void)undoBtnActon:(id)sender{
    
    if (self.viewModel.videoItemList.count > 0) {
        self.actionView.status = CEVehicleVideoCaptureStatusUndo;
        self.progressView.status = CEVehicleVideoCaptureStatusUndo;
//        [self.actionView.videoProgressView selectedProgress:self.viewModel.videoItemList.count-1];
        [self.progressView selectedProgress:self.viewModel.videoItemList.count-1];
    }
}
#pragma mark 删除
- (void)deleteBtnAction:(id)sender{
    [UIAlertController alertOnWindow:[UIApplication sharedApplication].windows.firstObject title:@"提示" message:@"是否删除此段视频？" submitTitle:@"删除" submitBtnStyle:UIAlertActionStyleDestructive submitBlock:^(UIAlertAction * _Nonnull action) {
        self.captureManager.videoTotalDuration = 0;
        for (NSInteger i = 0 ; i<self.viewModel.videoItemList.count; i++) {
            VideoCaptureItem *previousVideo = self.viewModel.videoItemList[i];
            [MediaManager deleteVideoFile:previousVideo.filePath];
        }
        [self.viewModel removePreviousVideoFile];
        self.viewModel.videoItemList = nil;
        
        //CEVehicleVideoCaptureStatusCaptured
        self.actionView.status = CEVehicleVideoCaptureStatusDelete;
        self.progressView.status = CEVehicleVideoCaptureStatusDelete;

    } cancelTitle:@"取消" cancleBtnStyle:UIAlertActionStyleCancel cancelBlock:nil completionBlock:nil];
}
-(void)uploadProgressView{
    self.captureManager.videoTotalDuration = 0;
    self.actionView.status = CEVehicleVideoCaptureStatusDelete;
    self.progressView.status = CEVehicleVideoCaptureStatusDelete;
}
#pragma mark 点击完成
- (void)doneBtnAction:(id)sender{
    if (self.viewModel.videoItemList.count == 0) {
        [UIAlertController alert:@"提示" message:@"请先拍摄视频" cancelTitle:@"确定" cancelBlock:nil completionBlock:nil];
    } else {
        NSString *videoFilename = [MediaManager getVideoFileName];
        NSString *outputVideoFilePath = [MediaManager filePath:videoFilename];
        kWeakSelf(weakSelf);
        [VideoCaptureManager mergeAndExportVideos:[self.viewModel.videoItemList bk_map:^id(VideoCaptureItem *obj) {
            return obj.filePath;
        }] outputPath:outputVideoFilePath presetName:nil outputFileType:nil completion:^(BOOL success, NSURL * _Nonnull hebingFileURL){
            if (success) {
                [self.viewModel.videoItemList enumerateObjectsUsingBlock:^(VideoCaptureItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    [MediaManager deleteVideoFile:obj.filePath];
                }];
                
                if (self.viewModel.getVehicleVideoBlock) {
                    self.viewModel.getVehicleVideoBlock(videoFilename);
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    VideoReleseController * vc = [VideoReleseController new];
                    vc.url= hebingFileURL;
                    [weakSelf.navigationController pushViewController:vc animated:YES];
                    [weakSelf uploadProgressView];
                    weakSelf.viewModel.videoItemList = nil;
                });
                DLog(@"%@",[NSThread currentThread]);
            } else {
                [UIAlertController alert:@"提示" message:@"视频合成失败！" cancelTitle:@"确定" cancelBlock:nil completionBlock:nil];
            }
        }];
    }
}
-(void)showTopBarItem:(BOOL)isShow{
    self.switchCameraBtn.hidden = isShow;
    self.backBtn.hidden = isShow;
    self.shootingBtn.hidden = isShow;
    self.photoAlbumBtn.hidden = isShow;
}
- (VideoCaptureManager *)captureManager
{
    if (!_captureManager) {
        _captureManager = [[VideoCaptureManager alloc] init];
        _captureManager.videoMaxDuration = 30;
        
        __weak typeof(self) _WeakSelf = self;
        _captureManager.didStartRecordingBlock = ^(NSString *currentFilePath) {
            _WeakSelf.actionView.status = CEVehicleVideoCaptureStatusCapturing;
            _WeakSelf.progressView.status = CEVehicleVideoCaptureStatusCapturing;

//            _WeakSelf.switchCameraBtn.hidden = YES;
//            _WeakSelf.flashBtn.hidden = YES;
//            _WeakSelf.backBtn.hidden = YES;
            [_WeakSelf showTopBarItem:YES];

            [_WeakSelf.viewModel addVideoFile:currentFilePath];
        };
        
        _captureManager.didRecordingBlock = ^(NSString *currentFilePath, NSTimeInterval videoCurrentDuration, NSTimeInterval videoTotalDuration) {
            
            [_WeakSelf.actionView updateDurationLabel:videoTotalDuration+videoCurrentDuration];
            
            VideoCaptureItem *item = _WeakSelf.viewModel.videoItemList.lastObject;
            item.duration = videoCurrentDuration;
            
//            [_WeakSelf.actionView.videoProgressView updateProgress:videoTotalDuration+videoCurrentDuration];
            [_WeakSelf.progressView updateProgress:videoTotalDuration+videoCurrentDuration];

        };
        
        _captureManager.didFinishRecordingBlock = ^(BOOL success, NSString *currentFilePath, NSTimeInterval videoCurrentDuration, NSTimeInterval videoTotalDuration, BOOL isOverDuration) {
            
//            _WeakSelf.switchCameraBtn.hidden = NO;
//            _WeakSelf.flashBtn.hidden = NO;
//            _WeakSelf.backBtn.hidden = NO;
            [_WeakSelf showTopBarItem:NO];

            if (success) {
                VideoCaptureItem *item = _WeakSelf.viewModel.videoItemList.lastObject;
                item.duration = videoCurrentDuration;

//                [_WeakSelf.actionView.videoProgressView addProgressItem:videoCurrentDuration];
                [_WeakSelf.progressView addProgressItem:videoCurrentDuration];

                if (isOverDuration) {
                    _WeakSelf.actionView.status = CEVehicleVideoCaptureStatusFinished;
                    _WeakSelf.progressView.status = CEVehicleVideoCaptureStatusFinished;
                } else {
                    _WeakSelf.actionView.status = CEVehicleVideoCaptureStatusCaptured;
                    _WeakSelf.progressView.status = CEVehicleVideoCaptureStatusCaptured;

                }
            } else {
                [_WeakSelf.viewModel removePreviousVideoFile];
//                [_WeakSelf.actionView.videoProgressView updateProgress:videoTotalDuration];
                [_WeakSelf.progressView updateProgress:videoTotalDuration];

                _WeakSelf.actionView.status = CEVehicleVideoCaptureStatusCaptured;
                _WeakSelf.progressView.status = CEVehicleVideoCaptureStatusCaptured;

            }
        };
    }
    return _captureManager;
}

- (VideoCaptureViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[VideoCaptureViewModel alloc] init];
    }
    return _viewModel;
}
-(void)dealloc{
    //销毁播放器
    [self.playView destroyPlayer];
    self.playView = nil;
}
@end
